<template>
  <div class="v-editor-prefix-setting g-pd-l-8">
    <span v-if="row.key !== PropertyKey.Customization_NamingRule_prefixRule">{{ row.value }}</span>
    <span v-else>{{ row.value && row.value.id ? $t(prefixRuleMap[row.value.id]) : "" }}</span>

    <div
      v-if="selected && !row.readonly && !isModelDisabled && !tableElementDisabled"
      class="g-pd-r-4 img-btn"
    >
      <img
        src="statics/icons/app/iconmore.png"
        class="g-pointer"
        @click="handleClick"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { PropertyRow } from "../../types";
import { Ref, inject } from "vue";
import { modelService } from "@/modules/model/service";
import { BusEvent, PropertyKey } from "@/constants/Enums";
import { ModelData } from "@/mdesign-lib/mdesign-app.umd.js";
import { prefixSettingWindow } from "@/modules/model/modals/nameRulesSetting";

const isModelDisabled = inject("isModelDisabled") as Ref<boolean>;

const props = defineProps<{
  row: PropertyRow;
  selected?: boolean;
  tableElementDisabled?: boolean;
}>();

let parentId = "";
let applyToElements: string[] = [];
let isNameUni = false;
let defaultName = "";
let prefixRule = "";
let separator = "";
let prefix = "";

const prefixRuleMap = {
  "PrefixEnumeration-noPrefix": "无前缀",
  "PrefixEnumeration-fixedPrefix": "固定前缀",
  "PrefixEnumeration-useOwnerName": "所有者名称作为前缀"
};

// 获取最新属性
async function getParamDefaultValue() {
  const list = await modelService.getModelProperties(app.activeProjectId, props.row.modelId);
  let ownerValue = list.find(item => item.key === PropertyKey.Element_owner)?.value as ModelData;
  parentId = ownerValue.id;

  list.forEach(item => {
    switch (item.key) {
      case PropertyKey.Customization_NamingRule_applyToElements:
        {
          let applyToElementsValue = item.value as ModelData[];
          applyToElements = applyToElementsValue.map(it => it.id) || [];
        }
        break;

      case PropertyKey.Customization_NamingRule_isNameUni:
        isNameUni = item.value as boolean;
        break;

      case PropertyKey.Customization_NamingRule_defaultName:
        defaultName = item.value as string;
        break;

      case PropertyKey.Customization_NamingRule_prefixRule:
        {
          let prefixRuleValue = item.value as ModelData;
          prefixRule = prefixRuleValue ? prefixRuleValue.id : "";
        }
        break;

      case PropertyKey.Customization_NamingRule_separator:
        separator = item.value as string;
        break;

      case PropertyKey.Customization_NamingRule_prefix:
        prefix = item.value as string;
        break;

      default:
        break;
    }
  });
}

async function handleClick() {
  await getParamDefaultValue();

  prefixSettingWindow
    .popup({
      projectId: app.activeProjectId,
      data: JSON.stringify({
        prefixRule,
        separator,
        prefix
      })
    })
    .then(async res => {
      if (res) {
        if (res.success && res.data) {
          const data = JSON.parse(res.data);

          const updateRes = await modelService.batchUpdateNamingRule({
            projectId: app.activeProjectId,
            parentId,
            namingRules: [
              {
                dataType: "update",
                nameRuleId: props.row.modelId,
                applyToElements,
                isNameUni,
                defaultName,
                prefixRule: data.prefixRule, // 用修改后的值
                separator: data.separator,
                prefix: data.prefix
              }
            ]
          });

          if (updateRes) {
            app.$bus.emit(BusEvent.REFRESH_PROPERTY, props.row.modelId);
          }
        }
      }
    });
}
</script>

<style lang="scss" scoped>
.v-editor-prefix-setting {
  height: 100%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .img-btn {
    display: flex;
    align-items: center;
  }
}
</style>
